package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.BackTime;

/**
 * A class used for managing back time
 */
public class BackTimeDAO {
	
    private Database db;
    
    public BackTimeDAO(Database db) {
        this.db = db;
    }
    
    public boolean hasBackTime(int pnSiteId) {
        String vaSql = "SELECT * FROM BACK_TIME WHERE SITE_ID = ?";
        List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
        
        if (result != null && result.size() > 0) {
            return true;
        } else {
            return false;
        }
    }
    
    public BackTime getBackTime(int pnSiteId) {
        String vaSql = "SELECT * FROM BACK_TIME WHERE SITE_ID = ?";
        List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
        
        if (result != null && result.size() > 0) {
        	int vnSiteId = Integer.parseInt(result.get(0).get("SITE_ID").toString());
        	String vaBackTimeDate = result.get(0).get("BACK_TIME_DATE").toString();
        	String vaExpireDate = result.get(0).get("EXPIRE_DATE").toString();
        	int vnInsertUserId = Integer.parseInt(result.get(0).get("INSERT_USER_ID").toString());
        	String vaInsertDate = result.get(0).get("INSERT_DATE").toString();
        	String vaInsertTime = result.get(0).get("INSERT_TIME").toString();
        	int vnUpdateUserId = Integer.parseInt(result.get(0).get("UPDATE_USER_ID").toString());
        	String vaUpdateDate = result.get(0).get("UPDATE_DATE").toString();
        	String vaUpdateTime = result.get(0).get("UPDATE_TIME").toString();
        	
        	BackTime backTime = new BackTime(vnSiteId, vaBackTimeDate, vaExpireDate,
        			vnInsertUserId, vaInsertDate,vaInsertTime,
        			vnUpdateUserId,vaUpdateDate,vaUpdateTime);
        	
            return backTime;
            
        } else {
            return null;
        }
    }
    
    public BackTime getExpireAndBackTimeDate(int pnSiteId) {
        String vaSql = "SELECT * FROM BACK_TIME WHERE site_id = ?";
        Map<String, Object> result = db.querySingle(vaSql,pnSiteId);    
    	BackTime voBackTime = new BackTime();
        if (result != null ) {
        	voBackTime.setVaBackTimeDate(result.get("BACK_TIME_DATE").toString());
        	voBackTime.setVaExpireDate(result.get("EXPIRE_DATE").toString());     	
            return voBackTime;          
        } else {
        	voBackTime = null;
        }
        return voBackTime;
    }

    public boolean insert(BackTime b)
    {
    	String vaSql = " insert into BACK_TIME (SITE_ID,BACK_TIME_DATE,EXPIRE_DATE,INSERT_USER_ID,INSERT_DATE,INSERT_TIME,UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME) " 
    			+ "values (?,?,?,?,?,?,?,?,?) ";
    	
    	try
    	{
    		db.insert(vaSql, b.getVnSiteId(), b.getVaBackTimeDate(), b.getVaExpireDate(), b.getVnInsertUserId(), b.getVaInsertDate(), b.getVaInsertTime(),
    				b.getVnUpdateUserId(), b.getVaUpdateDate(), b.getVaUpdateTime());
    	}
    	catch(Exception ex)
    	{
    		return false;
    	}
    	return true;
    }
    
    public boolean update(int pnSite_Id, String paBackTimeDate, String paExpireDate, 
    		int pnUpdateUserId, String paUpdateDate, String paUpdateTime)
    {
    	String vaSql = " update BACK_TIME set BACK_TIME_DATE = ?, EXPIRE_DATE = ?, UPDATE_USER_ID = ?, UPDATE_DATE = ?, UPDATE_TIME = ? " 
    			+ " where SITE_ID = ? ";
    	try
    	{
    		db.update(vaSql, paBackTimeDate, paExpireDate, pnUpdateUserId, paUpdateDate, paUpdateTime, pnSite_Id);
    	}
    	catch(Exception ex)
    	{
    		return false;
    	}
    	return true;
    }
    
    public boolean update(BackTime b)
    {
    	String vaSql = " update BACK_TIME set BACK_TIME_DATE = ?, EXPIRE_DATE = ?, UPDATE_USER_ID = ?, UPDATE_DATE = ?, UPDATE_TIME = ? " 
    			+ " where SITE_ID = ? ";
    	try
    	{
    		db.update(vaSql, b.getVaBackTimeDate(), b.getVaExpireDate(), b.getVnUpdateUserId(), b.getVaUpdateDate(), b.getVaUpdateTime(), b.getVnSiteId());
    	}
    	catch(Exception ex)
    	{
    		return false;
    	}
    	return true;
    }
    
}
